package com.aitoedu.server.controller.teacher;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.entity.Teacher;
import com.aitoedu.server.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/teacher")
@Slf4j
@CrossOrigin
@Tag(name = "教师端相关接口")
@SaCheckRole("教师")
public class TeacherController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/pl")
    @Operation(summary = "个人信息")
    public SaResult pl(){
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        Teacher teacher = (Teacher) redisTemplate.opsForValue().get("teacher_pl:"+id);
        if(teacher != null) return SaResult.ok().setData(teacher);
        teacher = teacherService.pl();
        redisTemplate.opsForValue().set("teacher_pl:"+id,teacher);
        return SaResult.ok().setData(teacher);
    }
}
